package com.lzx.iteam.util;

import android.os.Environment;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: Megan
 * Date: 2016年 11月 08日
 * Time: 下午3:23
 *
 * @QQ : 1234567890
 * **********************************
 */
public class FileUtils {
    private String SDPATH;

    public String getSDPATH() {
        return SDPATH;
    }

    public FileUtils() {
        //得到当前外部存储设备的目录
        SDPATH = Environment.getExternalStorageDirectory() + "/";
    }

    public File createSDFile(String fileName) throws IOException {
        File file = new File(SDPATH + fileName);
        file.createNewFile();
        return file;
    }

    /**
     * 在SD卡上创建目录
     */
    public File createSDDir(String dirName) {
        File dir = new File(SDPATH + dirName);
        dir.mkdir();
        return dir;
    }

    /**
     * 判断SD卡上文件夹是否存在
     */
    public boolean isFileExist(String fileName) {
        File file = new File(SDPATH + fileName);
        return file.exists();
    }

    /**
     * 将一个InputStream里面的数据写入到SD卡中
     *
     * @param path
     * @param fileName
     * @param input
     * @return
     */
    public File write2SDFromINput(String path, String fileName, InputStream input) {
        File file = null;
        OutputStream output = null;
        try {

            byte[] arr = new byte[1];
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(baos);
            int n = input.read(arr);
            while (n > 0) {
                bos.write(arr);
                n = input.read(arr);
            }
            bos.close();


            createSDDir(path);
            file = createSDFile(path + fileName);
            output = new FileOutputStream(file);
            output.write(baos.toByteArray());

            output.flush();
            baos.close();
            /*
createSDDir(path);
file = createSDFile(path + fileName);
output = new FileOutputStream(file);
byte buffer[] = new byte[10 * 1024*1024];
while((input.read(buffer)) != -1)
{
output.write(buffer);
}
output.flush();
*/
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {

                output.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return file;
    }

    /**
     * 删除指定目录下文件及目录
     * @param deleteThisPath
     * @param filePath
     * @return
     */
    public static void deleteFolderFile(String filePath, boolean deleteThisPath) {
        if (!StringUtil.isEmpty(filePath)) {
            try {
                File file = new File(filePath);
                if (file.isDirectory()) { // 处理目录
                    File files[] = file.listFiles();
                    for (int i = 0; i < files.length; i++) {
                        deleteFolderFile(files[i].getAbsolutePath(), true);
                    }
                }
                if (deleteThisPath) {
                    if (!file.isDirectory()) {// 如果是文件，删除
                        file.delete();
                    } else {    // 目录
                        if (file.listFiles().length == 0) {
                            // 目录下没有文件或者目录，删除
                            //   file.delete();  暂时不删除文件目录
                        }
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}
